package com.zgjkhis.domain;

import com.github.pagehelper.PageInfo;
import com.zgjkhis.dozer.service.IGenerator;
import com.zgjkhis.utils.SpringContextUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

/**
 * @author ：yxl
 * @date ：Created in 2020-08-04 16:55
 * @description：分页参数返回
 * @modified By：
 * @version: V1.0
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel("分页通用响应结果")
public class PageResult<T> implements Serializable {


    @ApiModelProperty("总数量")
    private long totalElements;

    @ApiModelProperty("内容")
    private List<T> content;

    public static <T> PageResult emptyPage() {
        return new PageResult<T>();
    }

    public static <T> PageResult<T> data(PageInfo<T> page) {
        PageResult<T> result = new PageResult<>();
        result.setContent(page.getList());
        result.setTotalElements(page.getTotal());
        return result;
    }

    public static <T> PageResult<T> data(PageInfo page, Class<T> tClass) {
        IGenerator generator = SpringContextUtils.getBean(IGenerator.class);
        PageResult<T> result = new PageResult<>();
        result.setContent(generator.convert(page.getList(), tClass));
        result.setTotalElements(page.getTotal());
        return result;
    }
}
